"""
Case Type   : cm_ctl可靠性
Case Name   : cm_ctl stop -n 故障测试-环境变量异常
Create At   : 2022-08-11
Owner       : opentestcase012
Description :
    1: 修改env文件中的$GAUSSHOME路径为错误路径
    2: source 环境变量,执行cm_ctl stop -n命令
    3: 恢复env文件中的$GAUSSHOME路径
    4: source 环境变量,再次执行cm_ctl stop -n命令
    5: 启动节点;
Expect      :
    1: 修改成功
    2: source成功，stop执行失败
    3: 恢复成功
    4: source成功，stop执行成功
    5: 成功
History     :
"""

import os
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != Primary_SH.get_node_num(), '需1主2备环境，否则不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_user = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.sta1_user = Node('Standby1DbUser')
        self.constant = Constant()
        self.common = Common()
        self.start_file = os.path.join(macro.GAUSSHOME_BIN_PATH,
                                       'cluster_manual_start')

    def test_cm(self):
        text = '----step1:修改env文件中的$GAUSSHOME路径为错误路径; expect:成功----'
        self.log.info(text)
        gausshome = os.path.dirname(macro.GAUSSHOME_BIN_PATH)
        gausshome_path = f"GAUSSHOME={gausshome}"
        newgausshome_path = f"GAUSSHOME={macro.GAUSSHOME_BIN_PATH}"
        try:
            sed_cmd = f"sed -i 's#{gausshome_path}#{newgausshome_path}#g' " \
                f"{macro.DB_ENV_PATH};" \
                f"cat {macro.DB_ENV_PATH};" \
                f"source {macro.DB_ENV_PATH};"
            self.log.info(sed_cmd)
            sed_msg = self.common.get_sh_result(self.sta1_user, sed_cmd)
            self.assertIn(f'export {newgausshome_path}', sed_msg, "执行失败:" + text)

            text = '----step2:source 环境变量,执行cm_ctl stop -n命令;' \
                   ' expect:source成功，stop执行失败----'
            self.log.info(text)
            stop_cmd = f'source {macro.DB_ENV_PATH};' \
                f'cm_ctl stop -n 2 ;'
            self.log.info(stop_cmd)
            stop_msg = self.common.get_sh_result(self.sta1_user, stop_cmd)
            self.assertIn("cm_ctl: command not found", stop_msg,
                          "执行失败:" + text)
        finally:
            text = '----step3:恢复env文件中的$GAUSSHOME路径; expect:成功----'
            self.log.info(text)
            sed_cmd = f"sed -i 's#{newgausshome_path}#{gausshome_path}#g' " \
                f"{macro.DB_ENV_PATH};" \
                f"cat {macro.DB_ENV_PATH};" \
                f"source {macro.DB_ENV_PATH};"
            self.log.info(sed_cmd)
            sed_msg = self.common.get_sh_result(self.sta1_user, sed_cmd)
            self.assertIn(f'export {gausshome_path}', sed_msg, "执行失败:" + text)

        text = '----step4:source 环境变量,再次执行cm_ctl stop -n命令;' \
               ' expect:source成功，stop执行成功----'
        self.log.info(text)
        self.log.info(stop_cmd)
        stop_msg = self.common.get_sh_result(self.sta1_user, stop_cmd)
        self.assertIn(self.constant.cm_stop_node_success_msg, stop_msg,
                      "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '-----step5:启动节点; expect:成功-----'
        self.log.info(text)
        time.sleep(10)
        start_cmd = f'source {macro.DB_ENV_PATH};' \
            f'cm_ctl start -n 2;' \
            f'sleep 5;' \
            f'cm_ctl query -i -v -C -d;'
        self.log.info(start_cmd)

        start_msg = self.common.get_sh_result(self.pri_user, start_cmd)
        status_msg = self.common.get_sh_result(self.pri_user,start_cmd)
        self.assertIn(self.constant.cm_start_node_success_msg, start_msg,
                      "执行失败:" + text)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, status_msg,
                      "执行失败:" + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
